// pages/activityCenter/bmSure.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "确认支付", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    btnText: '前往支付',
    openid: '',
    shareTitle: '',
    shareImg: '',
    title: '',
    time: '',
    address: '',
    pid: 0, //订单id
    activityInfo: [], //活动信息
    orderInfo: [], //订单信息
    kcName: '',
    successUrl: ''
  },
  // 关闭弹窗
  onConfimClose() {
    var that = this;
    wx.redirectTo({
      url: that.data.successUrl
    });
  },
  //支付
  pay() {
    //调起支付
    var that = this;

    wx.showLoading({
      title: '支付发起中...'
    });

    wx.request({
      url: that.data.baseUrl + 'createActivtyPayOrder.php',
      method: 'POST',
      data: {
        openid: that.data.openid,
        pid: that.data.pid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //后台请求成功 掉起支付
          var payMoney = res.data.payMoney;
          var payOrderNumber = res.data.payOrderNumber;
          wx.requestPayment({
            'timeStamp': res.data.data.timeStamp,
            'nonceStr': res.data.data.nonceStr,
            'package': res.data.data.package,
            'signType': res.data.data.signType,
            'paySign': res.data.data.paySign,
            'success': function (res) {
              console.log('success');
              var date = new Date();
              var year = date.getFullYear();
              var month = date.getMonth() + 1; // 月份从0开始，所以要加1
              var day = date.getDate();
              var hour = date.getHours();
              var minute = date.getMinutes();
              var second = date.getSeconds();
              if (hour < 10) {
                hour = '0' + hour;
              }
              if (minute < 10) {
                minute = '0' + minute;
              }
              if (second < 10) {
                second = '0' + second;
              }
              var payTime = year + "年" + month + "月" + day + "日 " + hour + ":" + minute + ":" + second;
              // 显示弹窗
              that.selectComponent('#confirmTtips').openPop();
              that.setData({
                successUrl:'/pages/activityCenter/paySuccess?payTime=' + payTime + "&payMoney=" + payMoney + "&payOrderNumber=" + payOrderNumber
              });
            },
            'fail': function (res) {
              console.log(res);
            },
            'complete': function (res) {
              console.log('complete');
            }
          });
        } else {
          //后台请求失败
          app.showTip(res.data.msg);
          return;
        }
      },
      fail: function (res) {
        wx.hideLoading();
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    var pid = options.pid;
    var time = options.time;
    if (pid == '' || pid == '0') {
      app.showTip('抱歉,未获取到订单id哦');
      setTimeout(function () {
        wx.navigateBack({
          delta: 1
        });
      }, 2000);
      return;
    }
    this.setData({
      pid: pid,
      time: time
    });

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getBmSurePageInfo(res.data.openid, pid);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getBmSurePageInfo(openid, pid);
    }
    //调用分享
    that.getShareInfo();
  },

  //获取本页数据
  getBmSurePageInfo(openid, pid) {
    var that = this;

    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getBmSurePageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'pid': pid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            activityInfo: res.data.activityInfo,
            orderInfo: res.data.orderInfo,
            kcName: res.data.kcName
          });
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})